
/**
 * Date:2019年6月4日上午11:28:50
 * Copyright (c) 2019, www.bwbroad.com All Rights Reserved.
 *
*/

package com.xue.im.client.ui;

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.Image;
import java.awt.SystemTray;
import java.awt.TrayIcon;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.List;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTabbedPane;
import javax.swing.SwingUtilities;

import com.xue.im.client.core.IMClientContext;
import com.xue.im.client.ui.list.UserCellRender;
import com.xue.im.client.ui.list.UserListModel;
import com.xue.im.common.vo.User;

public class UserListDialog extends JFrame {

	private static final long serialVersionUID = 1L;
	private final JPanel contentPanel = new JPanel();

	private JList<User> userList;// 用户列表
	private UserListModel userListModel;// 用户列表模型
	private JLabel lbUsername;// 用户名称
	private TrayIcon clientTrayIcon;// 系统托盘
	private JLabel lbImage;

	public static void main(String[] args) {
		// 测试用
		try {
			UserListDialog dialog = new UserListDialog();
			dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
			dialog.setVisible(true);

		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	private static class UserListDialogHolder {
		private static UserListDialog instance = new UserListDialog();
	}

	public static UserListDialog getInstance() {
		return UserListDialogHolder.instance;
	}

	/**
	 * Create the dialog.
	 */
	private UserListDialog() {
		setTitle("IM2019");
		setResizable(false);
		setIconImage(IMClientContext.client.getImage());
		setBounds(100, 100, 300, 650);
		getContentPane().setLayout(new BorderLayout());
		getContentPane().add(contentPanel, BorderLayout.CENTER);
		contentPanel.setLayout(new BorderLayout(0, 0));

		JPanel panel = new JPanel();
		contentPanel.add(panel, BorderLayout.NORTH);
		panel.setLayout(new BorderLayout(10, 10));

		lbImage = new JLabel(IMClientContext.man);
		panel.add(lbImage, BorderLayout.WEST);

		JPanel panel_1 = new JPanel();
		panel.add(panel_1, BorderLayout.CENTER);
		panel_1.setLayout(new BorderLayout(0, 0));

		lbUsername = new JLabel("用户名");
		lbUsername.setFont(new Font("Dialog", Font.BOLD, 14));
		panel_1.add(lbUsername, BorderLayout.CENTER);

		JLabel lblNewLabel_2 = new JLabel("简介");
		panel_1.add(lblNewLabel_2, BorderLayout.SOUTH);

		JPanel panel_2 = new JPanel();
		contentPanel.add(panel_2, BorderLayout.SOUTH);
		panel_2.setLayout(new BorderLayout(0, 0));

		JPanel panel_3 = new JPanel(new FlowLayout(FlowLayout.LEFT));
		panel_2.add(panel_3, BorderLayout.CENTER);

		JButton btSet = new JButton("设置");
		btSet.setEnabled(false);
		panel_3.add(btSet);

		btSet.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
			}
		});

		JButton btSearch = new JButton("查 找");
		btSearch.setEnabled(false);
		panel_3.add(btSearch);

		JPanel panel_4 = new JPanel();
		panel_2.add(panel_4, BorderLayout.EAST);

		JButton btClose = new JButton("退 出");
		panel_4.add(btClose);

		btClose.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				shutdown();// 退出程序
			}
		});

		JTabbedPane tabbedPane = new JTabbedPane(JTabbedPane.TOP);
		contentPanel.add(tabbedPane, BorderLayout.CENTER);

		JPanel pUserlistOut = new JPanel();
		tabbedPane.addTab("我的好友", null, pUserlistOut, null);
		pUserlistOut.setLayout(new BorderLayout(0, 0));

		// 用户列表
		userList = new JList<>();
		userListModel = new UserListModel();
		userList.setModel(userListModel);
		// 定义用户列表如何渲染
		UserCellRender userCellRender = new UserCellRender();
		// 设置JList的渲染器为我们自己构建的渲染器
		userList.setCellRenderer(userCellRender);
		// 添加到带滚动条的pane中
		JScrollPane pUserlist = new JScrollPane(userList);
		pUserlistOut.add(pUserlist, BorderLayout.CENTER);
		MouseListener mouseListener = new MouseAdapter() {
			public void mouseClicked(MouseEvent e) {
				if (e.getClickCount() == 2) {
					// int index = userList.locationToIndex(e.getPoint());
					User u = userList.getSelectedValue();
					u.clearMsgCount();// 清空消息计数
					ChatDialog ct = ChatDialog.getInstance();
					ct.refreshUser(u);// 设置当前用户信息
					ct.refreshMessage();
					ct.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
					ct.setVisible(true);
				}
			}
		};
		// 添加双击事件
		userList.addMouseListener(mouseListener);
		// --------------------------------------------------

		JPanel pMessageList = new JPanel();
		tabbedPane.addTab("消息列表", null, pMessageList, null);

		JLabel lblNewLabel_3 = new JLabel("暂未实现");
		pMessageList.add(lblNewLabel_3);
		setLocation(WindowXY.getXY(this.getSize()));
		// 添加托盘
		if (SystemTray.isSupported()) {
			showSystemTray();
		}

		// 设置在前边显示
		// this.setAlwaysOnTop(!this.isAlwaysOnTop());
		// 覆盖关闭事件
		/*
		 * addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent
		 * e) { if (SystemTray.isSupported()) {// 如果不支持托盘 showSystemTray(); } else {
		 * shutdown(); } } });
		 */

	}

	private void showSystemTray() {
		// 获得系统托盘对象
		SystemTray systemTray = SystemTray.getSystemTray();
		// 系统托盘
		// 判断是否支持系统托盘
		// 获取图片所在的URL
		// 实例化图像对象
		ImageIcon icon = IMClientContext.client;
		// 获得Image对象
		Image image = icon.getImage();
		// 创建托盘图标
		clientTrayIcon = new TrayIcon(image);
		// 为托盘添加鼠标适配器
		clientTrayIcon.addMouseListener(new MouseAdapter() {
			// 鼠标事件
			public void mouseClicked(MouseEvent e) {
				// 判断是否双击了鼠标
				if (e.getClickCount() == 2) {
					setVisible(true);
					// systemTray.remove(clientTrayIcon);
				}
			}
		});
		// 添加工具提示文本
		clientTrayIcon.setToolTip("IM2019运行中！");
		// 创建弹出菜单
		// PopupMenu popupMenu = new PopupMenu();
		// popupMenu.add(new MenuItem("Close"));
		// 为托盘图标加弹出菜弹
		// clientTrayIcon.setPopupMenu(popupMenu);

		try {
			// 为系统托盘加托盘图标
			systemTray.add(clientTrayIcon);
		} catch (Exception e) {
			e.printStackTrace();
		}

	}

	// 退出程序
	private void shutdown() {
		// 清理上下文对象
		IMClientContext.get().shutdown();
		// 关闭
		System.exit(0);
	}

	// 刷新用户显示信息
	public void refreshUser(User user) {
		SwingUtilities.invokeLater(() -> {
			lbUsername.setText(user.getUsername());
			if (user.getSex() == 0) {
				lbImage.setIcon(IMClientContext.woman);
			}
		});
	}

	// 刷新用户列表
	public void refreshUserList(List<User> list) {
		SwingUtilities.invokeLater(() -> {
			userListModel.setList(list);
			userList.updateUI();// 这个是关键刷新语句
		});
	}

	public void refreshUserList() {
		SwingUtilities.invokeLater(() -> {
			userList.updateUI();// 这个是关键刷新语句
		});
	}

}
